﻿using System;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;

namespace iss.sg4210.se18.team6s.Controllers
{
    [Authorize()]
    public class SkillController : Controller
    {
        //
        // GET: /Skill/

        public ActionResult Index()
        {
            return View(DataAccessFactory.Instance.Skill.FindAllSkills());
        }

        //
        // GET: /Skill/Details/5

        public ActionResult Details(int id)
        {
            return View(DataAccessFactory.Instance.Skill.GetSkill(id));
        }

        //
        // GET: /Skill/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Skill/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Skill skill = new Skill();
                UpdateModel(skill, collection);
                DataAccessFactory.Instance.Skill.AddSkill(skill);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Skill/Edit/5

        public ActionResult Edit(int id)
        {
            return View(DataAccessFactory.Instance.Skill.GetSkill(id));
        }

        //
        // POST: /Skill/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Skill skill = DataAccessFactory.Instance.Skill.GetSkill(id);
                UpdateModel(skill);
                DataAccessFactory.Instance.Skill.UpdateSkill(skill);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Skill/Delete/5

        public ActionResult Delete(int id)
        {
            return View(DataAccessFactory.Instance.Skill.GetSkill(id));
        }

        //
        // POST: /Skill/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                DataAccessFactory.Instance.Skill.DeleteSkill(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        public ActionResult SkillTypeList(int id)
        {
            if (id < 0)
                ViewData["SkillTypeList"] = new SelectList(DataAccessFactory.Instance.SkillType.FindAllSkillTypes(), "Id", "Name");
            else
                ViewData["SkillTypeList"] = new SelectList(DataAccessFactory.Instance.SkillType.FindAllSkillTypes(), "Id", "Name", DataAccessFactory.Instance.SkillType.GetSkillType(id).Id);

            return View("SkillTypeDropDownList");
        }
    }

    [Authorize()]
    public class SkillTypeController : Controller
    {
        //
        // GET: /SkillType/

        public ActionResult Index()
        {
            return View(DataAccessFactory.Instance.SkillType.FindAllSkillTypes());
        }

        //
        // GET: /SkillType/Details/5

        public ActionResult Details(int id)
        {
            return View(DataAccessFactory.Instance.SkillType.GetSkillType(id));
        }

        //
        // GET: /SkillType/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SkillType/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                SkillType skillType = new SkillType();
                UpdateModel(skillType, collection);
                DataAccessFactory.Instance.SkillType.AddSkillType(skillType);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /SkillType/Edit/5

        public ActionResult Edit(int id)
        {
            return View(DataAccessFactory.Instance.SkillType.GetSkillType(id));
        }

        //
        // POST: /SkillType/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                SkillType skillType = DataAccessFactory.Instance.SkillType.GetSkillType(id);
                UpdateModel(skillType);
                DataAccessFactory.Instance.SkillType.UpdateSkillType(skillType);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /SkillType/Delete/5

        public ActionResult Delete(int id)
        {
            return View(DataAccessFactory.Instance.SkillType.GetSkillType(id));
        }

        //
        // POST: /SkillType/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            SkillType skillType = null;
            try
            {
                // TODO: Add delete logic here
                skillType = DataAccessFactory.Instance.SkillType.GetSkillType(id);
                if (skillType.Skills.Count == 0)
                {
                    DataAccessFactory.Instance.SkillType.DeleteSkillType(id);
                    return RedirectToAction("Index");
                }
                else
                    throw new Exception("Skills are tagged to this Skilltype");
            }
            catch (Exception ex)
            {
                // ModelState.AddModelError(string.Empty, ex.Message);
                return View(skillType);
            }
        }
    }
}
